<?php
//
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$busys = array('0'=>'空闲中','1'=>'已入座','2'=>'已下单','3'=>'已付款');
$return=array();
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	if($_RQ['store_id']){
		$store_id = intval($_RQ['store_id']);
		$this->session('store_id',$store_id);
	}
	if(!$store_id){
		$store_id=$this->session('store_id');
	}
	if(!$store_id){
		exi('请先选择门店！','error','jy_meye/index/');
	}
	$store = pdo_get('jy_meye_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	$categories = pdo_getall('jy_weishop_goods_group',array('parentid'=>0,'isshow'=>1,'uniacid'=>$_SESSION['uniacid']),'','','paixu desc');
	$carts=$this->session('carts');
	$allnum =0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
		}
	}else{
		$carts=array();
	}
	foreach($categories as $ck=>&$v){
		$v['goods']=pdo_getall('jy_weishop_goods',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>$v['id']),'','','paixu desc');
		foreach($v['goods'] as $k=>&$g){
			$store_goods=$this->pdo_get('store_goods',array('store_id'=>$store_id,'goods_id'=>$g['id']),array('total','status'));
			if(!$store_goods || !$store_goods['status'] || !$store_goods['total']){
				unset($v['goods'][$k]);
			}
			$g=array_merge($g,$store_goods);
			$virtual = pdo_getall('jy_weishop_order_goods',array('goodsid'=>$g['id'],'uniacid'=>$_SESSION['uniacid'],'storeid'=>$store_id,'plugin_name'=>'jy_meye'),array('total'));
			$g['virtual_all'] =0;
			if(!empty($virtual)){
				foreach($virtual as &$sale){
					$g['virtual_all'] +=$sale['total'];
				}
			}
			$g['package']=$g['package']?json_decode($g['package'],true):array();
			if(!empty($g['package'])){
				foreach($g['package'] as $k=>&$p){
					$p['price']=$p['price']?$p['price']:$g['price'];
					$cartspackage = $carts[$g['id']]['package'];
					$p['num']=$cartspackage[$k];
					$g['cartnum'] +=$p['num'];
					$p['pprice']=$p['num']*$p['price'];
					$goodsprice += $p['num']*$p['price'];
				}	
			}else{
				$g['cartnum']= $carts[$g['id']]['num'] >0 ? $carts[$g['id']]['num'] : 0;
				$goodsprice += $g['cartnum']*$g['price'];
			}
		}
		if(count($v['goods'])==0){
			unset($categories[$ck]);
		}
	}
	$allprice = $goodsprice - $youhui;
	$return['store_id']=$store_id;
	$return['categories']=$categories;
	$return['allnum']=$allnum;
	$return['allprice']=$allprice;
	$return['carts']=$carts;
	set_html('title',$store['title'].'-美业');
	exi($return);
}
if($_RQ['op']=='diancai'){
	$goodid=intval($_RQ['goodid']);
	$num=intval($_RQ['num']);
	$packageid=intval($_RQ['packageid']);
	$carts=$this->session('carts');
	if(!$carts){
		$carts=array();
	}
	$carts[$goodid]['num']=$num;
	if($num == 0){
		unset($carts[$goodid]);
	}
	$goodinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodid));
	$packageinfo=json_decode($goodinfo['package'],true);
	$package=$carts[$goodid]['package'];
	if(!empty($packageinfo)){
		foreach($packageinfo as $k=>$v){
			if($k == $packageid){
				if($_RQ['plus']){
					$package[$k] +=1;
				}else{
					$package[$k] -=1;
					if($package[$k] == 0){
						unset($packageinfo[$i]);
					}
				}
			}
		}
		$carts[$goodid]['package'] = $package;
	}
	$carts=$this->session('carts',$carts);
	exi($carts);
}
?>